package com.zuimei.zuimei.activities;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.base.BaseActivity;
import com.zuimei.zuimei.beans.FindAppItem1Bean;
import com.zuimei.zuimei.beans.Item1DescBean;
import com.zuimei.zuimei.http.HttpHelper;
import com.zuimei.zuimei.jsonutil.ParseFindAppJson;
import com.zuimei.zuimei.utils.ConstantUtil;
import com.zuimei.zuimei.utils.ScreenUtil;
import com.zuimei.zuimei.utils.ShareSdkUtil;
import com.zuimei.zuimei.utils.URLImageGetter;
import com.zuimei.zuimei.widgets.DownLoadDialog;
import com.zuimei.zuimei.widgets.MyScrollView;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by 任聪 on 2017/2/4.
 */
public class CategoryCotentActivity extends BaseActivity {

    @Bind(R.id.iv_head)
    ImageView iv_head;
    @Bind(R.id.iv_edxq_sm)
    ImageView iv_edxq_sm;
    @Bind(R.id.tv_edxq_title)
    TextView tv_edxq_title;
    @Bind(R.id.tv_edxq_sub)
    TextView tv_edxq_sub;
    @Bind(R.id.rl_head_container)
    RelativeLayout rl_head_container;
    @Bind(R.id.ll_tags_container)
    LinearLayout ll_tags_container;
    @Bind(R.id.ll_btn_container)
    LinearLayout ll_btn_container;
    @Bind(R.id.bt_edxq_fav)
    Button bt_edxq_fav;
    @Bind(R.id.bt_edxq_share)
    Button bt_edxq_share;
    @Bind(R.id.bt_edxq_down)
    Button bt_edxq_down;
    @Bind(R.id.tv_desc)
    TextView tv_desc;
    @Bind(R.id.ll_desc_img_container)
    LinearLayout ll_desc_img_container;
    @Bind(R.id.tv_edxq_tui)
    TextView tv_edxq_tui;
    @Bind(R.id.sv_content_container)
    MyScrollView sv_content_container;
    @Bind(R.id.ibtn_back)
    ImageButton ibtn_back;
    @Bind(R.id.ibtn_app)
    ImageButton ibtn_app;
    @Bind(R.id.iv_flower)
    ImageView iv_flower;
    @Bind(R.id.ll_download)
    LinearLayout ll_download;
    @Bind(R.id.iv_edxq_deng)
    ImageView iv_edxq_deng;
    @Bind(R.id.ibtn_fav)
    ImageButton ibtn_fav;
    @Bind(R.id.ibtn_share)
    ImageButton ibtn_share;
    @Bind(R.id.ibtn_down)
    ImageButton ibtn_down;
    private FindAppItem1Bean.AppsBean mApp;
    private Item1DescBean mDesc;
    private int y;
    private int mY;
    private int mShowY;
    private int id;

    @Override
    public int getLayoutId() {
        return R.layout.activity_item1_desc;
    }

    @Override
    protected void init() {
        id =  getIntent().getExtras().getInt("id");
        iv_head.measure(0, 0);
        rl_head_container.measure(0, 0);
        ibtn_back.measure(0, 0);
        //获取到的ivhead的高度是96，未解
        mShowY = 360 + rl_head_container.getMeasuredHeight() - ibtn_back.getMeasuredHeight();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        mY = ScreenUtil.getSrceenHeight(getBaseContext());
        int[] position = new int[2];
        ibtn_app.getLocationOnScreen(position);
        y = position[1];
    }


    @Override
    protected void loadData() {
        new HttpHelper(this).getData(String.format(ConstantUtil.FIND_APP_ITEM1_DESC_URL, id), new HttpHelper.OnLoadCompletedListener() {
            @Override
            public void onSuccess(String result) {
                mDesc = ParseFindAppJson.getItem1Desc(result);
                Picasso.with(getBaseContext())
                        .load(mDesc.getCover_image())
                        .config(Bitmap.Config.RGB_565)
                        .into(iv_head, new Callback() {
                            @Override
                            public void onSuccess() {
                                sv_content_container.setEnlarge(iv_head);
                            }

                            @Override
                            public void onError() {

                            }
                        });
                Picasso.with(getBaseContext())
                        .load(mDesc.getAuthor_avatar_url())
                        .config(Bitmap.Config.RGB_565)
                        .into(iv_edxq_sm);
                Picasso.with(getBaseContext())
                        .load(mDesc.getAuthor_avatar_url())
                        .config(Bitmap.Config.RGB_565)
                        .into(iv_edxq_deng);
                tv_edxq_title.setText(mDesc.getTitle());
                String subTitle = mDesc.getSub_title().length() > 16 ? mDesc.getSub_title()
                        .substring(0, 16) + "..." : mDesc.getSub_title();
                tv_edxq_sub.setText(subTitle);
                tv_desc.setMovementMethod(LinkMovementMethod.getInstance());//加这句才能让里面的超链接生效
                tv_desc.setText(Html.fromHtml(mDesc.getContent(), new URLImageGetter
                        (CategoryCotentActivity.this, tv_desc), null));
            }

            @Override
            public void onFailure() {

            }
        });
        sv_content_container.setOnScrollListener(mShowY + ibtn_back.getMeasuredHeight(), mShowY,
                new MyScrollView.OnScrollListener() {
                    @Override
                    public void showAppBtn() {
                        ibtn_app.animate().translationYBy(y).y(mY).setDuration(500)
                                .setInterpolator(new BounceInterpolator()).start();
                    }

                    @Override
                    public void hideAppBtn() {
                        ibtn_app.animate().translationYBy(mY).y(y).setDuration(500)
                                .setInterpolator(new BounceInterpolator()).start();
                    }

                    @Override
                    public void upShow() {
                        ll_btn_container.setVisibility(View.INVISIBLE);
                        ibtn_fav.setVisibility(View.VISIBLE);
                        ibtn_share.setVisibility(View.VISIBLE);
                        ibtn_down.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void downShow() {
                        //滑动到与回退按钮底部
                        ibtn_fav.setVisibility(View.INVISIBLE);
                        ibtn_share.setVisibility(View.INVISIBLE);
                        ibtn_down.setVisibility(View.INVISIBLE);
                        ll_btn_container.setVisibility(View.VISIBLE);
                    }
                });

    }

    @OnClick({R.id.ibtn_back, R.id.ll_download, R.id.bt_edxq_down, R.id.ibtn_down, R.id
            .bt_edxq_share, R.id.ibtn_share})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ibtn_back:
                finish();
                break;
            case R.id.ll_download:
            case R.id.bt_edxq_down:
            case R.id.ibtn_down:
                download();
                break;
            case R.id.bt_edxq_share:
            case R.id.ibtn_share:
                new ShareSdkUtil().showShare(this);
                break;
        }
    }

    private void download() {
        DownLoadDialog dialog = new DownLoadDialog();
        Bundle bundle = new Bundle();
        if (!TextUtils.isEmpty(mDesc.getDownload_url())) {
            bundle.putString("googleplay", mDesc.getDownload_url());
        }
        bundle.putString("title", mDesc.getTitle());
        bundle.putString("size", mDesc.getSize());
        if (mDesc.getOther_download_url().size() != 0) {
            for (int i = 0; i < mDesc.getOther_download_url().size(); i++) {
                String name = mDesc.getOther_download_url().get(i).getApp_market_verbose_name();
                if ("豌豆荚".equals(name)) {
                    bundle.putString("wandoujia", mDesc.getOther_download_url().get(i)
                            .getDownload_url());
                }
                if ("直接下载".equals(name)) {
                    bundle.putString("direct", mDesc.getOther_download_url().get(i)
                            .getDownload_url());
                }
            }
        }
        dialog.setArguments(bundle);
        dialog.show(getFragmentManager(), "download_dialog");
    }
}
